package com.heima.behavior.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.heima.behavior.dto.BehaviorDto;
import com.heima.behavior.entity.ApBehaviorEntry;
import com.heima.behavior.mapper.ApBehaviorEntryMapper;
import com.heima.behavior.service.IApBehaviorEntryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.dto.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Handler;

/**
 * <p>
 * APP行为实体表,一个行为实体可能是用户或者设备，或者其它 服务实现类
 * </p>
 *
 * @author Mr_Su
 * @since 2022-07-07
 */
@Service
public class ApBehaviorEntryServiceImpl extends ServiceImpl<ApBehaviorEntryMapper, ApBehaviorEntry> implements IApBehaviorEntryService {

    @Override
    public ApBehaviorEntry getBehaviorEntryByUserIdOrEquipmentId(Integer userId, String equipmentId) {
        if (userId == 0){ // 意味着没有登录,根据设备号获取entryId
            LambdaQueryWrapper<ApBehaviorEntry> lqwApBehaviorEntry = new LambdaQueryWrapper<>();
            lqwApBehaviorEntry.eq(ApBehaviorEntry::getEntryId,equipmentId);
            ApBehaviorEntry one = this.getOne(lqwApBehaviorEntry);
            if (one == null) {
                one = new ApBehaviorEntry();
                one.setEntryId(equipmentId);
                one.setCreatedTime(new Date());
                one.setType(0);
                this.save(one);
            }
            return one;
        }else {
            LambdaQueryWrapper<ApBehaviorEntry> lqwApBehaviorEntry = new LambdaQueryWrapper<>();
            lqwApBehaviorEntry.eq(ApBehaviorEntry::getEntryId,userId.toString());
            ApBehaviorEntry one = this.getOne(lqwApBehaviorEntry);
            if (one == null) {
                one = new ApBehaviorEntry();
                one.setEntryId(userId.toString());
                one.setCreatedTime(new Date());
                one.setType(1);
                this.save(one);
            }
            return one;
        }
    }

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Override
    public ResponseResult loadArticleBehavior(BehaviorDto dto) {

        /**
         * { "code":200,
         *   "errorMessage":"操作成功",
         *   "data":{
         *      "isfollow": true,
         *      "islike": true,
         *      "isunlike": false,
         *      "iscollection": true
         *       }
         * }
         */

        Map map = new HashMap<>();
        map.put("isfollow",redisTemplate.hasKey("saveFollowBehavior" + "_" + dto.getUserId() + "_" + dto.getEquipmentId() + "_"+ dto.getAuthorId() + "_" + dto.getArticleId()));
        map.put("islike",redisTemplate.hasKey("saveLikesBehavior" + "_" + dto.getUserId() + "_" + dto.getEquipmentId() + "_null_" + dto.getArticleId()));
        map.put("isunlike",redisTemplate.hasKey("saveUnLikesBehavior" + "_" + dto.getUserId() + "_" + dto.getEquipmentId() + "_null_" + dto.getArticleId()));
        map.put("iscollection",redisTemplate.hasKey("saveCollectionBehavior" + "_" + dto.getUserId() + "_" + dto.getEquipmentId() + "_null_" + dto.getArticleId()));
        return ResponseResult.okResult(map);
    }
}
